package com.lagou.demo3;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.windows.GlobalWindow;
import org.apache.flink.util.Collector;

import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.Random;

/*
基于事件驱动
 */
public class CountWindowDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//        DataStreamSource<String> data = env.socketTextStream("zb26105", 7777);


        //1、添加数据源
        DataStreamSource<String> data = env.addSource(new SourceFunction<String>() {
            int count = 0;

            @Override
            public void run(SourceContext<String> ctx) throws Exception {
                while (true) {
                    ctx.collect(count + "号数据源");
                    count++;
                    Thread.sleep(1000);
                }
            }
            @Override
            public void cancel() {

            }
        });

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");

        //2、转换数据
        SingleOutputStreamOperator<Tuple3<String, String, Integer>> maped = data.map(new MapFunction<String, Tuple3<String, String, Integer>>() {
            @Override
            public Tuple3<String, String, Integer> map(String value) throws Exception {
                //获取系统当前时间
                long l = System.currentTimeMillis();
                String dataTime = sdf.format(l);
                Random random = new Random();
                int randomNum = random.nextInt(5);

                return new Tuple3<>(value, dataTime, randomNum);
            }
        });


        //3、分组
        KeyedStream<Tuple3<String, String, Integer>, String> keyed = maped.keyBy(value -> value.f0);

        //4、获取窗口
        //基于事件驱动，每隔2个事件，触发一次计算，本次窗口的大小为3，代表窗口里的每种事件最多为3个
        //我们是对keyed的数据进行countWindow,也就是当根据keyBy(0)分组之后，数据的数量达到3时进行输出，如果我们设置的是countWindow(3)
        WindowedStream<Tuple3<String, String, Integer>, String, GlobalWindow> countWindow = keyed.countWindow(3,1);

        //5、对窗口数据进行操作
        SingleOutputStreamOperator<String> applyed = countWindow.apply(new WindowFunction<Tuple3<String, String, Integer>, String, String, GlobalWindow>() {
            @Override
            public void apply(String s, GlobalWindow window, Iterable<Tuple3<String, String, Integer>> input, Collector<String> out) throws Exception {
                String key = s;
                Iterator<Tuple3<String, String, Integer>> iterator = input.iterator();
                StringBuilder sb = new StringBuilder();
                while (iterator.hasNext()) {
                    Tuple3<String, String, Integer> next = iterator.next();
                    sb.append(next.f0 + "......" + next.f1 + "......" + next.f2);
                }
                out.collect(sb.toString());

            }
        });

        applyed.print();
        env.execute();


    }
}
